#include "CoreApp.h"
#include <iostream>

CoreApp::CoreApp() : isInitialized_(false) {
}

CoreApp::~CoreApp() {
    cleanup();
}

bool CoreApp::initialize() {
    if (isInitialized_) {
        return true;
    }

    try {
        // 初始化模块A
        moduleA_.setInfo("CoreApp initialized ModuleA");
        std::cout << "ModuleA initialized with info: " << moduleA_.getInfo() << std::endl;
        
        // 使用模块B处理模块A的信息
        std::string result = moduleB_.processModuleAInfo(moduleA_);
        std::cout << "ModuleB processed ModuleA info: " << result << std::endl;

        isInitialized_ = true;
        return true;
    } catch (const std::exception& e) {
        std::cerr << "Initialization failed: " << e.what() << std::endl;
        return false;
    }
}

void CoreApp::run() {
    if (!isInitialized_) {
        std::cout << "Error: Application not initialized!" << std::endl;
        return;
    }

    try {
        // 演示模块间的交互
        std::cout << "\n=== Application Running ===" << std::endl;
        
        // 更新模块A的信息
        moduleA_.setInfo("Updated ModuleA Info from CoreApp");
        std::cout << "Updated ModuleA info: " << moduleA_.getInfo() << std::endl;
        
        // 让模块B处理新的信息
        std::string newResult = moduleB_.processModuleAInfo(moduleA_);
        std::cout << "ModuleB processed new info: " << newResult << std::endl;
        
        // 显示最终状态
        std::cout << "ModuleB final status: " << moduleB_.getStatus() << std::endl;
        std::cout << "=== Application Finished ===" << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Runtime error: " << e.what() << std::endl;
    }
}

void CoreApp::cleanup() {
    if (!isInitialized_) {
        return;
    }

    try {
        // 清理资源
        std::cout << "\nCleaning up resources..." << std::endl;
        // 这里可以添加更多的清理逻辑
        isInitialized_ = false;
    } catch (const std::exception& e) {
        std::cerr << "Cleanup error: " << e.what() << std::endl;
    }
} 